"""
Base classes for actions.
"""

import sys
import os
import wx
from LMG.GUI.Base.menu import Menu

class Action:
    """
    Template for actions.
    """
    def __init__(self, 
                 filename = None, 
                 shortdesc = None, 
                 longdesc = None, 
                 menudesc = None, 
                 kind = wx.ITEM_NORMAL, 
                 id = None):

        # Menu bitmap flags
        self.defaultbmp  = id is not None and wx.Platform == "__WXGTK__" 
        
        if id is None:
            self.id = wx.NewId()
        else:
            self.id = id
        
        if shortdesc is not None:
            self.shortdesc = shortdesc
        else:
            self.shortdesc = ""
            
        if longdesc is not None:
            self.longdesc = longdesc
        else:
            self.longdesc = self.shortdesc
            
        if menudesc is not None:
            self.menudesc = menudesc
        else:
            self.menudesc = self.shortdesc
        
        self.kind = kind
        
        self.filename = filename
        self.initBitmap()
            
        self.toolbars = []
        self.menus = []

    def isBitmapAllowed(self):
        return utility.config.Read("allowmenubitmaps", "boolean")

    def hasDefaultBitmap(self):
        return self.defaultbmp
    
    def initBitmap(self):
        if self.filename is None:
            self.bitmap = None
            self.bitmapsmall = None  
        else:
            bitmap = utility.makeBitmap('toolbar', self.filename)
            self.bitmap32 = wx.ImageFromBitmap(bitmap).Scale(32,32).ConvertToBitmap()
            self.bitmap24 = wx.ImageFromBitmap(bitmap).Scale(24,24).ConvertToBitmap()
            self.bitmap16 = wx.ImageFromBitmap(bitmap).Scale(16,16).ConvertToBitmap()
            self.bitmapsmall = self.bitmap16
            self.setBitmap()
            del bitmap
        
    def setBitmap(self):
        size = 8 * (utility.config.Read('tb_size', "int") + 2)
        self.bitmap = getattr(self, "bitmap" + str(size))
            
    def action(self, event = None):
        pass
        
    def addToMenu(self, menu, bindto = None, forceBitmap = False):
        if bindto is None:
            bindto = menu
        elif sys.platform[:5] == "linux":
            menu.Bind(wx.EVT_MENU, self.action, id = self.id)
        bindto.Bind(wx.EVT_MENU, self.action, id = self.id)
        
        if isinstance(bindto, wx.Menu):
            text = self.menudesc.split("\t")[0]
        else:
            text = self.menudesc
            
        if forceBitmap:
            item = wx.MenuItem(menu, self.id, text)
        else:
            item = wx.MenuItem(menu, self.id, text, kind = self.kind)

        if self.bitmapsmall is not None and not self.hasDefaultBitmap() and \
           ((self.kind == wx.ITEM_NORMAL and self.isBitmapAllowed()) or forceBitmap) :
            item.SetBitmap(self.bitmapsmall)
            
        menu.AppendItem(item)

        if not menu in self.menus and not isinstance(bindto, wx.Menu):
            self.menus.append(menu)

        return self.id
        
    def removeFromToolbar(self, toolbar):
        if toolbar in self.toolbars:
            self.toolbars.remove(toolbar)
            removed = toolbar.DeleteTool(self.id)
            if removed:
                toolbar.toolcount -= 1

    def enableTool(self, enabled = True):
        for toolbar in self.toolbars:
            toolbar.EnableTool(self.id, enabled)
        
    def addToToolbar(self, toolbar):
        if (toolbar.firsttime):
            #Find size of images so it will be dynamics
            width = self.bitmap.GetWidth() + toolbar.hspacing
            height = self.bitmap.GetHeight() + toolbar.vspacing
            toolbar.SetToolBitmapSize(wx.Size(width, height))
            toolbar.firsttime = False
                
        tool = toolbar.AddLabelTool(self.id,
                                    self.shortdesc,                                          
                                    self.bitmap,
                                    kind = self.kind,
                                    shortHelp = self.shortdesc, 
                                    longHelp = self.longdesc)

            
        toolbar.Bind(wx.EVT_TOOL, self.action, tool)
        
        if not toolbar in self.toolbars:
            self.toolbars.append(toolbar)
        
        return tool
        
        
################################################################

class ActionMenu(Action):
    """
    A menu to contain actions.
    """
    def __init__(self,
                 filename = None,
                 shortdesc = None,
                 longdesc = None,
                 menudesc = None, 
                 subactions = None,
		 id = None):
        Action.__init__(self,
                        filename,
                        shortdesc,
                        longdesc,
                        menudesc,
			id = id)
                           
        if subactions is None:
            subactions = []
        self.subactions = subactions
                           
    def addToMenu(self, menu, bindto = None):       
        if bindto is None:
            bindto = menu

        submenu = Menu()

        for actionid in self.subactions:
            if actionid == -1:
                submenu.AppendSeparator()
            elif actionid in utility.actions:
                action = utility.actions[actionid]
                action.addToMenu(submenu, bindto)
                
        # wx.Menu and wx.MenuBar have different methods
        # for appending submenus
        if isinstance(menu, wx.Menu):
            item = wx.MenuItem(menu, self.id, self.menudesc, subMenu = submenu)
            if self.bitmapsmall is not None and not self.hasDefaultBitmap() and self.isBitmapAllowed():
                item.SetBitmap(self.bitmapsmall)
            menu.AppendItem(item)
            
        elif isinstance(menu, wx.MenuBar):
            menu.Append(submenu, self.menudesc)
        
        return self.id
    
    def addToToolbar(self, toolbar):
        if (toolbar.firsttime):
            #Find size of images so it will be dynamics
            width = self.bitmap.GetWidth() + toolbar.hspacing
            height = self.bitmap.GetHeight() + toolbar.vspacing
            toolbar.SetToolBitmapSize(wx.Size(width, height))
            toolbar.firsttime = False
                

        tool = toolbar.AddLabelTool(self.id,
                                    self.shortdesc,                                          
                                    self.bitmap,
                                    kind = self.kind,
                                    shortHelp = self.shortdesc, 
                                    longHelp = self.longdesc)

            
        toolbar.Bind(wx.EVT_TOOL, self.action, tool)
        
        if not toolbar in self.toolbars:
            self.toolbars.append(toolbar)
        
        return tool
    
    def action(self, event = None):
        menu = Menu()
        for actionid in self.subactions:
            if actionid == -1:
                menu.AppendSeparator()
            elif actionid in utility.actions:
                action = utility.actions[actionid]
                action.addToMenu(menu)
        utility.window.PopupMenu(menu)
